package com.twu.biblioteca.view.console.resolver;

import com.twu.biblioteca.entity.Book;
import com.twu.biblioteca.enums.MapKey;
import com.twu.biblioteca.view.console.ViewResolver;

import java.util.List;
import java.util.Map;

public class BookListViewResolver implements ViewResolver {
  public static final String HEADER =
      "-------------------------------------------\n" +
      "|       Title                Author       |\n" +
      "-------------------------------------------\n";
  public static final String END_LIE =
      "-------------------------------------------\n";

  private static final String BODY =
      "%d. %-19s      %-14s\n";

  public String getView(Map<MapKey, Object> model) {
    List<Book> bookList = (List<Book>) model.get(MapKey.Books);
    return HEADER + getBody(bookList) + END_LIE;
  }

  private String getBody(List<Book> bookList) {
    String body = "";
    int index = 1;
    for (Book book : bookList) {
      body += String.format(BODY, index, book.getTitle(), book.getAuthor());
      index += 1;
    }
    return body;
  }
}
